package cn.edu.htu;

import cn.edu.htu.entity.Dept;
import cn.edu.htu.entity.User;
import cn.edu.htu.mapper.DeptMapper;
import cn.edu.htu.mapper.UserMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageRowBounds;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 描述: test02
 *
 * @Package cn.edu.htu
 * @Author dakai
 * @Data 2021/7/9 17:42
 * @Version V1.0
 * Excitation 将来的你，一定会感谢现在的你
 */
public class test02 {
    SqlSessionFactory sqlSessionFactory;

    @Before
    public void before() {
        //将全局配置文件的xml构建成SqlSessionFactory
        String config = "mybatis-config.xml";
        //将xml构建成输入流
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(config);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //构建SqlSessionFactory
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    /**
     * 分页之Page
     */
    @Test
    public void test01() {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            //Mybatis在getMapper就会给我们创建jdk动态代理
            UserMapper mapper = session.getMapper(UserMapper.class);
            PageHelper.startPage(1,10);
            Page<User> user = mapper.queryUser();
            System.out.println(user);
    }
}

    /**
     * 分页之PageInfo
     */
    @Test
    public void test02() {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            //Mybatis在getMapper就会给我们创建jdk动态代理
            UserMapper mapper = session.getMapper(UserMapper.class);
            PageHelper.startPage(1,10);
            List<User> user = mapper.queryUser1();
            PageInfo pageInfo = new PageInfo(user);
            System.out.println(pageInfo);
            System.out.println("当前页:"+pageInfo.getPageNum());
            System.out.println("总条数:"+pageInfo.getTotal());
            System.out.println("是否是第一页:"+pageInfo.isIsFirstPage());
            System.out.println("是否最后一页:"+pageInfo.isIsLastPage());
            System.out.println("页码导航:"+ Arrays.toString(pageInfo.getNavigatepageNums()));
            System.out.print("页码导航:");
            for(int i:pageInfo.getNavigatepageNums()){
                System.out.print(i+"\t");
            }
        }
    }


    /**
     * 基于mybatis的伪分页
     * 使用该方法需要将mybatis-config核心配置文件中（
     *
     * <!--    插件：可以把它理解成mybatis的拦截器，可以拦截四大对象，可以拦截SQL，给SQL加一些公共的功能-->
     *     <plugins>
     * <!--        注册分页插件-->
     *         <plugin interceptor="com.github.pagehelper.PageInterceptor" >
     * <!--            helperDialect（可省略），用来设置数据库的发言，默认会自动检查当前数据库环境使用的数据库-->
     *             <property name="helperDialect" value="mysql"/>
     *         </plugin>
     *     </plugins>
     *
     *     ）注释掉
     */
    @Test
    public void test03() {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            //Mybatis在getMapper就会给我们创建jdk动态代理
            UserMapper mapper = session.getMapper(UserMapper.class);
            List<User> user = mapper.queryUser2(new RowBounds(0,10));

            System.out.println(user);
            }
        }
    }